package com.example.myapplication.ui.movie

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.myapplication.R


class CinemaAdapter (val cinemalist : List<Cinema_info>, val context:Context, val movie_name:String):
    RecyclerView.Adapter<CinemaAdapter.ViewHolder>(){

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view){
        val cName : TextView = view.findViewById(R.id.cName)
        val cAddress : TextView = view.findViewById(R.id.cAddress)
        val cRecentTime : TextView = view.findViewById(R.id.cRecentTime)
        val cPrice : TextView = view.findViewById(R.id.cPrice)
        val cDistance : TextView = view.findViewById(R.id.cDistance)
        val cBetween : View = view.findViewById(R.id.cBetween)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.cinema_info, parent, false)
        val viewHolder = ViewHolder(view)

        view.setOnClickListener {
            Toast.makeText(parent.context, "click date_info", Toast.LENGTH_LONG).show()
            val position = viewHolder.adapterPosition
            val info = cinemalist[position]

            val mIntent = Intent(context, Cinema_ChooseTimeSlot::class.java)
            mIntent.putExtra("cinema_name", cinemalist[position].cName)
            mIntent.putExtra("cinema_address", cinemalist[position].cAddress)
            mIntent.putExtra("movie_name", movie_name)
            mIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
            context.startActivity(mIntent)


            //startActivity(Intent(parent.context, Cinema_ChooseTimeSlot::class.java))
        }


        return viewHolder
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val cName = cinemalist[position].cName
        val cAddress = cinemalist[position].cAddress
        val cRecentTime = cinemalist[position].cRecentTime
        val cPrice = cinemalist[position].cPrice
        val cDistance = cinemalist[position].cDistance
        holder.cName.text = cName
        holder.cAddress.text = cAddress
        holder.cRecentTime.text = cRecentTime
        holder.cPrice.text = "${cPrice}"
        holder.cDistance.text = "${cDistance}km"
    }

    override fun getItemCount(): Int {
        return cinemalist.size
    }
}